package com.dl.mall.controller.admin;

import com.dl.mall.annotation.MethodInfo;
import com.dl.mall.annotation.SaAdminCheckRole;
import com.dl.mall.entity.Brand;
import com.dl.mall.service.admin.BrandService_Admin;
import com.dl.mall.service.user.BrandService;
import com.dl.mall.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DL
 * @create 2022-04-11 11:51
 */
@Slf4j
@RestController()
@RequestMapping("/admin/brand")
@SaAdminCheckRole(value = {"super","admin","user"})
@Api(tags="管理员品牌controller")
public class BrandController_Admin {

    @Resource
    private BrandService brandService;

    @Resource
    private BrandService_Admin brandServiceAdmin;

    @ApiOperation(value="根据 分区id 获取品牌，用于懒加载")
    @MethodInfo
    @GetMapping("/getBrand")
    public Result getBrand(Integer categoryId){
        List<Brand> brandList = brandService.getBrandByCategoryId(categoryId);
        if (brandList != null){
            return Result.ok("查询品牌成功").data("brandList", brandList);
        }else {
            return Result.error("查询品牌失败");
        }
    }

    @ApiOperation(value="根据 分区id和品牌名 查询品牌，用于防止重复")
    @MethodInfo
    @GetMapping("/isExit")
    public Result getBrand(Integer categoryId, String brandName){
        boolean existBrand = brandServiceAdmin.isExistBrand(categoryId, brandName);
        return Result.ok().data("result", existBrand);
    }


    @ApiOperation(value="上传品牌")
    @PostMapping("/add")
    public Result addBrand(@Param("brandFormStr") String brandFormStr, @Param("mainImg") MultipartFile brandImg){
        boolean addBrand = brandServiceAdmin.addBrand(brandFormStr, brandImg);

        if (addBrand){
            return Result.ok("增加品牌成功");
        }else {
            return Result.error("增加品牌失败");
        }
    }

}
